import java.util.Arrays;

/**
 * @Author ZhangCuirong
 * @Date 2025/9/4 11:43
 * @description:
 */
public class Solution238 {
    public int[] productExceptSelf(int[] nums) {
        int[] prefix = new int[nums.length];
        int[] suffix = new int[nums.length];
        int[] res = new int[nums.length];
        for (int i = 0; i < nums.length; i++) {
            prefix[i] = ((i - 1 >= 0) ? prefix[i - 1] : 1) * nums[i];
        }
        for (int i = nums.length - 1; i >= 0; i--) {
            suffix[i] = ((i < nums.length - 1) ? suffix[i + 1] : 1) * nums[i];
        }
        for (int i = 0; i < nums.length; i++) {
            if (i == 0) {
                res[i] = suffix[i + 1];
            } else if (i == nums.length - 1) {
                res[i] = prefix[i - 1];
            } else {
                res[i] = prefix[i - 1] * suffix[i + 1];
            }
        }
        return res;
    }

    public static void main(String[] args) {
        Solution238 solution = new Solution238();
        System.out.println(Arrays.toString(solution.productExceptSelf(new int[]{1, 2, 3, 4})));
        System.out.println(Arrays.toString(solution.productExceptSelf(new int[]{-1, 1, 0, -3, 3})));
    }

}
